<?php
/**
 * 聚水潭
 * @author           程顺伟
 * @lastmodify       2022-06-26
 */

defined('IN_YZMPHP') or exit('Access Denied');
new_session_start();
yzm_base::load_common('class/jst.class.php');
class wxapp2024{
    public function __construct(){
		header("Access-Control-Allow-Origin: *");//允许所有地址跨域请求
    }
	
	public function get_config(){
		$jst_list = get_jst_list();
		echo json_encode($jst_list);
	}

	public function get_suggest(){
		$db = D('jst_inventorys');
		$sql = 'select sum(a) total_qty,brand from (SELECT qty-order_lock as a,brand FROM yzm_jst_inventorys where brand in("增致牛仔 ZENGZHI","法派FAPAI","JEEP","东方骆驼","衣品天成","美丽衣橱","虎都","安瑞井","米菲 MIFFY","ICE") HAVING a<0) i GROUP BY brand';
		$list = $db->fetch_all($db->query($sql));
		$data = [];
		foreach($list as $v){
			//采购途中
			$sql = 'SELECT sum( purchase_qty ) total_purchase_qty  FROM yzm_jst_inventorys  where brand = "'.$v['brand'].'"';
			$result = $db->fetch_array($db->query($sql));
			$v['total_purchase_qty'] = $result[0]['total_purchase_qty']??0;
			
			$sql = 'SELECT sum( purchase_qty ) purchase_qty FROM (SELECT i.qty - i.order_lock AS a,i.sku_id,purchase_qty FROM yzm_jst_inventorys i WHERE ( i.brand = "'.$v['brand'].'" ) HAVING a < 0 ) c';
			$result = $db->fetch_array($db->query($sql));
			$v['purchase_qty'] = $result[0]['purchase_qty']??0;
			
			//待采购
			$sql = 'SELECT sum(a) wait_purchase_qty FROM (SELECT i.qty - i.order_lock+purchase_qty AS a,i.sku_id,purchase_qty FROM yzm_jst_inventorys i WHERE ( i.brand = "'.$v['brand'].'" ) HAVING a < 0 ) c';
			$result = $db->fetch_array($db->query($sql));
			
			$v['wait_purchase_qty'] = $result[0]['wait_purchase_qty']??0;
			
			if($v['brand'] != '佑晟共享仓'){
				$data[] = $v;
			}
		}
		echo json_encode($data);
	}
	
	public function login(){
		$admin = D('admin');
		$post = $_POST;
		$res = $admin->where(array('adminname' => $post['username']))->find();
		if(!$res){
			return_json(['status'=>0,'message'=>'用户名、密码错误']);
		}
		if(password($post['password']) != $res['password']){	
			return_json(['status'=>0,'message'=>'用户名、密码错误']);
		}
		if($res['roleid'] != 1){
			return_json(['status'=>0,'message'=>'没有权限']);
		}
		return_json(['status'=>1,'message'=>'登录成功']);
	}
}